<?php
/**
 * Created by IntelliJ IDEA.
 * User: haoya_Coding_one
 * Date: 2019/3/23
 * Time: 11:05
 */

class Core
{
    function run(){
        spl_autoload_register(array($this, 'loadClass'));

        $this->setReporting();
        $this->removeMagicQuotes();
        $this->unRegisterGlobals();
        $this->route();
    }

    function route(){
        $controllerName='Index';
        $action='index';
        $queryParam=array();
        if (!empty($_GET['url'])){
            $url=$_GET['url'];
            $urlCrumb=explode('/', $url);
            //获取控制器名
            $controllerName=ucfirst($urlCrumb[0]);
            array_shift($urlCrumb);
            //获取方法名，动作名
            $action=empty($urlCrumb)?'index':$urlCrumb[0];
            array_shift($urlCrumb);
            //获取rest风格的参数
            $queryParam=empty($urlCrumb)?$queryParam:$urlCrumb;
        }
        $controller=$controllerName.'Controller';
        $dispatch=new $controller($controllerName, $action);
        if ((int)method_exists($controller, $action)){
            call_user_func_array(array($dispatch, $action), $queryParam);
        }else{
            exit('方法不存在:['.$controller.'->'.$action.']');
        }
    }

    /**
     * 检测自定义全局变量('register_globals')并移除
     */
    function unRegisterGlobals(){
        if (ini_get('register_globals')){
            $array=array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
            foreach ($array as $value){
                foreach ($GLOBALS[$value] as $key => $val){
                    if ($val === $GLOBALS[$key]){
                        unset($GLOBALS[$key]);
                    }
                }
            }
        }
    }
    /**
     * 检测环境
     */
    function setReporting(){
        if (APP_DEBUG === true){
            error_reporting(E_ALL);
            ini_set('display_errors', 'On');
        }else{
            error_reporting(E_ALL);
            ini_set('display_errors', 'Off');
            ini_set('log_errors', 'On');
            ini_set('error_log', RUNTIME_PATH.'logs/error.log');
        }
    }

    /**
     * @param $value
     * @return array|string
     * 递归删除敏感字符
     */
    function stripSlashesDeep($value){
        $value=is_array($value)?array_map('stripSlashesDeep', $value):stripslashes($value);
        return $value;
    }

    /**
     * 检测敏感字符
     */
    function removeMagicQuotes(){
        if (get_magic_quotes_gpc()){
            $_GET=$this->stripSlashesDeep($_GET);
            $_POST=$this->stripSlashesDeep($_POST);
            $_COOKIE=$this->stripSlashesDeep($_COOKIE);
            $_SESSION=$this->stripSlashesDeep($_SESSION);
        }
    }

    static function loadClass($class){
        $frameClass=FRAME_PATH . $class . '.php';
        $controller=APP_PATH . 'application/controllers/' . $class . '.php';
        $model=APP_PATH . 'application/models/' . $class . '.php';
        if (file_exists($frameClass)){
            //加载框架核心类
            include $frameClass;
        }elseif (file_exists($controller)){
            //加载控制器类
            include $controller;
        }elseif (file_exists($model)){
            //加载模型类
            include $model;
        }
    }
}
